<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Commodity_ajax extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->database();
		$this->seg_exp = segment_explode($this->uri->uri_string());
// 		$this->load->model('commodity/commodity_model');
	}


	public function _remap($method, $params = array())
	{
// 		$this->load->view('/common/header');

		if( method_exists($this, $method) ){
//echo  'method_exists-$method: '.$method.'<br>';
			//$this->{"{$method}"}();
			return call_user_func_array(array($this, $method), $params);

		} else if ($method == 'page') {//※ページ
//echo  '$method'.$method.'<br>';
			if( in_array('list', $this->seg_exp) ) {
				$this->list($params);
			} else  {

				$this->index();
			}
		}

// 		$this->load->view('/common/footer');
	}



	public function index() {

		$this->load->view('commodity/commodity_header');

	}
	public function get_commodity() {

		$page = $this->input->post('page');
		//ページ処理 start
		$paging_data['tbl_nm'] = 'commodity_detail';
		$paging_data['where'] = '';
		$paging_data['select'] = '*';
		$paging_data['orderItem'] = 'commodity_code';
		$paging_data['orderType'] = 'ASC';
		$paging_data['search_url']='';
		$data = make_pagination_ajax($page, $paging_data);
		//ページ処理 end
//var_dump($data);
		//category
		$this->load->model('/admin/category_mo', 'category_mo');
		$data['category_tree'] = $this->category_mo->get_all_category('0', 1);

		//$this->load->view('commodity/commodity', array('rs' => $data));
		$this->load->view('commodity/commodity_body', $data);
		// 		$this->load->view('foot');
	}

	//検索orカテゴリクリック
	public function  lists($params=null) { //旧：($comodity_code = ''){

		$where = array();
		//ヘッダの検索からの場合
		if( in_array('q', $this->seg_exp) )	{

			$paging_data['search_word'] = $search_word = urldecode($this->security->xss_clean(url_explode($this->seg_exp, 'q')));
			//주소에서 q 제거. 뷰에서 사용
			$this->seg_exp =  url_delete($this->seg_exp, 'q');
			$paging_data['search_url'] = "/q/".$search_word;
//echo  "q/".$search_word;

// 			$where = "commodity_code like '%".$search_word."%'";
//  			$where = " (commodity_code like '%".$search_word."%' or sku_code like '%".
//  				$search_word."%' )"; うだ//."%' or standard_detail1_code like '%".$search_word."%' or standard_detail2_code like '%".$search_word."%') ";
			$where['or_like'] = array('commodity_code' => $search_word,
										'sku_code' => $search_word,
										'standard_detail1_code' => $search_word,
										'standard_detail2_code' => $search_word);

			$paging_data['tbl_nm'] = 'commodity_detail';
			$paging_data['where'] = $where;
			$paging_data['select'] = '*';
			$paging_data['orderItem'] = 'commodity_code';
			$paging_data['orderType'] = 'ASC';

		//カテゴリクリックの場合
		}else{

			//$paging_data['search_word'] = $search_word = urldecode($this->security->xss_clean(url_explode($this->seg_exp, 'q')));
			//주소에서 q 제거. 뷰에서 사용
			//$this->seg_exp =  url_delete($this->seg_exp, 'q');
			//$paging_data['search_url'] = "/q/".$search_word;

			//category
			$sql = "SELECT commodity_detail.* FROM commodity_detail
			JOIN commodity_header ON commodity_header.represent_sku_code = commodity_detail.sku_code
			JOIN category_commodity ON commodity_header.commodity_code = category_commodity.commodity_code
			AND category_commodity.category_code = ? ";
			//ORDER BY commodity_detail.sku_code ASC LIMIT 10";

			$paging_data['search_word'] = $search_word =$params;
			$paging_data['search_url'] = "";
			$paging_data['sql'] =$sql;
// 			$paging_data['join'] = array('commodity_header' => 'commodity_header.represent_sku_code = commodity_detail.sku_code'
// 										,'category_commodity' => "commodity_header.commodity_code = category_commodity.commodity_code AND category_commodity.category_code = '".$search_word."'"

// 										);
			//$where[] = array('category_commodity.category_code' => $search_word);
			$paging_data['param'] = $search_word;
			$paging_data['orderItem'] = 'commodity_detail.sku_code';
			$paging_data['orderType'] = 'ASC';
		}


//echo 'xxx'.$paging_data['search_url'] ;

		//ページ処理 start
		$data = make_pagination($this->seg_exp, $paging_data);
		//ページ処理 end

		//category
		$this->load->model('/admin/category_mo', 'category_mo');
		$data['category_tree'] = $this->category_mo->get_all_category('0', 1);



// 		$this->load->model("commodity_model");
// 		$data['search_total'] = $total = $this->common_model->search_total($post);


// 		$data = $this->commodity_model->lists($comodity_code);
		// 		$this->load->view('head');
		// 		$data = array('sku_code'=>$sku_code);
		// 		$this->load->view('get',$commodity_data);
		$this->load->view('commodity/commodity_list', $data);//array('rs'=>$data));
		// 		$this->load->view('foot');
	}



// 	public function add_cart(){
// 		$this->load->library('cart');

// 		$cart_data = array(
// 				'commodity_code' => $this->input->post('commodity_code'),
// 				'id'        	 => $this->input->post('commodity_code'),
// 				'qty'       	 => $this->input->post('qty'),
// 				'price'     	 => $this->input->post('1000'),
// 				'name'      	 => $this->input->post('1000'),
// 				'options'   	 => array(
// 						'Size' 			 => $this->input->post('size'),
// 						'Color' 		 => $this->input->post('color')
// 				)
// 		);

// 		$this->cart->insert($cart_data);

// 		$result = array(
// 				'result'=> 0
// 		);


// 	}

//----------------------OLD----------------------------------------

	public function  searchdetail($comodity_code){
		// 		$this->load->database();
		$this->load->model("commodity_model");
		$data = $this->commodity_model->get($comodity_code);
		// 		$this->load->view('head');
		// 		$data = array('sku_code'=>$sku_code);
		// 		$this->load->view('get',$commodity_data);
		$this->load->view('commodity/commodity_search_detail',array('rs'=>$data));
		// 		$this->load->view('foot');
	}

	public function  bootstrap(){
		$this->load->view('bootstrap');
	}

	// 	public function index(){
	// 		$this->search();
	// 	}

// 	public function  get($comodity_code){
// 		// 		$this->load->da$pagination_linkstabase();
// 		$this->load->model("commodity_model");
// 		$data = $this->commodity_model->get($comodity_code);
// 		// 		$this->load->view('head');
// 		// 		$data = array('sku_code'=>$sku_code);
// 		// 		$this->load->view('get',$commodity_data);
// 		$this->load->view('commodity/commodity_detail',array('rs'=>$data));
// 		// 		$this->load->view('foot');
// 	}

	// 	public function  lists($comodity_code){
	// 		// 		$this->load->database();
	// 		$this->load->model("commodity_model");
	// 		$data = $this->commodity_model->lists($comodity_code);
	// 		// 		$this->load->view('head');
	// 		// 		$data = array('sku_code'=>$sku_code);
	// 		// 		$this->load->view('get',$commodity_data);
	// 		$this->load->view('commodity/commodity_list',array('rs'=>$data));
	// 		// 		$this->load->view('foot');
	// 	}



	// 	public function search(){
	// 		$this->load->view('common/header');
	// 		$list = $this->commodity_model->searchList();
	// 		$this->load->view('commodity/list1', array('list'=>$list));
	// 		$this->load->view('common/footer');
	// 	}

	// 	public function detail($sku_code){
	// 		$this->load->view('common/header');
	// 		$detail = $this->commodity_model->search_detail($sku_code);
	// 		$this->load->view('commodity/detail', array('detail'=>$detail));
	// 		$this->load->view('common/footer');
	// 	}

	// 	public function get_commodity($params = null) {

	// 		$this->url_seg = $this->seg_exp;

	// 		// 		foreach($data as $entry) {
	// 		// // 			var_dump($entry->id);
	// 		// 			var_dump($entry);
	// 		// 		}
	// // 		$this->load->view('head');

	// 		//あと,共通化
	// 		if($this->uri->segment(4) == 'page')
		// 		{
		// 			$data['page_account']= $page = 1;
		// 		}
	// 		else
		// 		{
		// 			//페이징
		// 			if( in_array('page', $this->seg_exp) )
			// 			{
			// 				$data['page_account'] = $page = urldecode($this->security->xss_clean(url_explode($this->seg_exp, 'page')));
			// 			}
		// 			else
			// 			{
			// 				$data['page_account'] = $page = 1;
			// 			}
		// 		}

	// 		$rp    = 20; //리스트 갯수(表示数)
	// 		$limit = 6; //보여줄 페이지수

	// 		$start = (($page-1) * $rp);

	// 		$data['rs']           = $this->common_model->gets('commodity_detail', $start, $rp);//$this->commodity_model->gets($start, $rp);
	// 		$data['search_total'] = $total = $this->common_model->gets_total('commodity_detail'); //$this->commodity_model->gets_total();
	// 		if( in_array('page', $this->seg_exp) )
		// 		{

		// 			$urls = implode('/', url_delete($this->url_seg, 'page'));
		// 		}
	// 		else
		// 		{
		// 			$urls = implode('/', $this->url_seg);
		// 		}

	// //TEST確認用
	// //echo $urls.':'.$page.':'.$rp.':'.$total.':'.$limit.':'.$page;

	//  		$data['pagination_links'] = pagination_common($urls."/page", paging($page,$rp,$total,$limit));


	// 		//category
	// 		$this->load->model('/admin/category_mo', 'category_mo');
	// 		$data['category_tree'] = $this->category_mo->get_all_category('0', 1);

	// 		//$this->load->view('commodity/commodity', array('rs' => $data));
	// 		$this->load->view('commodity/commodity', $data);
	// // 		$this->load->view('foot');
	// 	}

}

